/**
 * Created by aifece on 15/1/22.
 */
;(function(){
  function AjaxAddress(option)
  {
    var _this = this;

    this._default = {
      'url'          : siteUrl+'/provincial/get_provincial',
      'parentEle'    : '#addressSlt',
      'parentView'   : '',
      'province'     : '#prov',
      'city'         : '#city',
      'district'     : '#dist',
      'prov_title'   : '省/直辖市',
      'city_title'   : '市',
      'dist_title'   : '区/县',
      'default_prov' : 0,
      'default_city' : 0,
      'default_dist' : 0
    };

    this.option = $.extend(this._default, option);

    // 初始化选项
    this.resetDefault(this.option.default_prov, this.option.default_city, this.option.default_dist);

    // 绑定事件
    $(this.option.parentEle).on('change', this.option.province, function(){
      var currId = $(this).val();
      _this.changeList(currId, 1);
      if ($(this).val() == 'null' && _this.option.parentView != '') {
        $(_this.option.city).closest(_this.option.parentView).hide();
      } else {
        $(_this.option.city).closest(_this.option.parentView).show();
      }
      $(_this.option.district).closest(_this.option.parentView).hide();
    });
    $(this.option.parentEle).on('change', this.option.city, function(){
      var currId = $(this).val();
      _this.changeList(currId, 2);
      if ($(this).val() == 'null' && _this.option.parentView != '') {
        $(_this.option.district).closest(_this.option.parentView).hide();
      } else {
        $(_this.option.district).closest(_this.option.parentView).show();
      }
    });
  }

  // change菜单
  AjaxAddress.prototype.changeList = function(currId, _index)
  {
    this.option.default_city = 0;
    this.option.default_dist = 0;

    if (_index == 1) {
      this.createOption($(this.option.city), 2, currId);
      $(this.option.district).html('<option value="null">'+this.option.dist_title+'</option>');
    } else {
      this.createOption($(this.option.district), 3, currId);
    }
  }

  // 设置联动菜单
  AjaxAddress.prototype.resetDefault = function(prov, city, dist)
  {
    this.option.default_prov = prov;
    this.option.default_city = city;
    this.option.default_dist = dist;

    this.createOption($(this.option.province), 1, 0);
    if (prov > 0) {
      this.createOption($(this.option.city), 2, prov);
    } else {
      $(this.option.city).html('<option value="null">'+this.option.city_title+'</option>');
      if (this.option.parentView != '') {
        $(this.option.city).closest(this.option.parentView).hide();
        $(this.option.district).closest(this.option.parentView).hide();
      }
    }
    if (city > 0) {
      this.createOption($(this.option.district), 3, city);
    } else {
      $(this.option.district).html('<option value="null">'+this.option.dist_title+'</option>');
      if (this.option.parentView != '') {
        $(this.option.district).closest(this.option.parentView).hide();
      }
    }
  }

  // 通过父级Id 创建option列表
  AjaxAddress.prototype.createOption = function(obj, _index, parentId)
  {
    var _this = this;
    $.post(this.option.url, { parent_id:parentId }, function(json){
      if (_index == 2) {
        obj.html('<option value="null">'+_this.option.city_title+'</option>');
      } else if (_index == 3) {
        obj.html('<option value="null">'+_this.option.dist_title+'</option>');
      } else {
        obj.html('<option value="null">'+_this.option.prov_title+'</option>');
      }
      if (!json) {
        return;
      }
      var optionLength = json.length;
      if (optionLength > 0) {
        for (var i = 0; i< optionLength; i++) {
          var selected = '';
          if ((_index == 1 && json[i].id == _this.option.default_prov) ||
            (_index == 2 && json[i].id == _this.option.default_city) ||
            (_index == 3 && json[i].id == _this.option.default_dist)) {
            selected = 'selected';
          }
          obj.append('<option value="'+json[i].id+'" '+selected+'>'+json[i].title+'</option>');
        }
      }
    }, 'json');
  }

  window.AjaxAddress = AjaxAddress;
})();